package com.gking.resourcePool.utils.commoUtil;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * 自定义加解密方法
 */
public class CustomEncryptorUtil {

    private static final String ALGORITHM = "AES";

    //在AES加密中，密钥的长度必须是16字节（128位）、24字节（192位）或32字节（256位）
    private static final String SECRET_KEY = "GKing is No1 ^_^ haha !!"; // 这是你的密钥，请替换为自己的密钥,

    /**
     * 加密明文
     *
     * @param plainText 明文
     * @return s
     */
    public static String encryption(String plainText) {
        try {
            if (plainText == null || plainText.isEmpty()) return null;

            SecretKeySpec secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    /**
     * 解密密文
     *
     * @param encryptedText 密文
     * @return s
     */
    public static String decrypt(String encryptedText) {
        try {
            if (encryptedText == null || encryptedText.isEmpty()) return null;

            SecretKeySpec secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            return new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    /**
     * 加密分割的字符串
     *
     * @param str       要分割加密的字符串
     * @param chunkSize 步长
     * @return 返回分割加密的拼接长字符串
     */
    public static String encryptionSplitString(String str, int chunkSize) {
        if (str == null || str.isEmpty()) return null;

        int length = str.length();
        List<String> chunks = new ArrayList<>();

        // 将字符串分割成指定长度的子字符串
        for (int i = 0; i <= length - chunkSize; i++)
            chunks.add(str.substring(i, i + chunkSize));

        // 将分割的字符串进行加密然后拼接
        StringBuilder encryptionStr = new StringBuilder();
        for (String chunk : chunks) encryptionStr.append(encryption(chunk));

        return encryptionStr.toString();
    }

    /**
     * 加密分割的字符串
     *
     * @param str       要分割加密的字符串
     * @param chunkSize 步长
     * @return 返回分割加密的字符串列表
     */
    public static List<String> encryptionSplitStringToList(String str, int chunkSize) {
        if (str == null || str.isEmpty()) return null;

        int length = str.length();
        List<String> chunks = new ArrayList<>();

        // 将字符串分割成指定长度的子字符串
        for (int i = 0; i <= length - chunkSize; i++)
            chunks.add(encryption(str.substring(i, i + chunkSize)));

        return chunks;
    }

}

